﻿using System;
using System.Collections.Generic;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Tokens
{
    struct RgceAreaRel
    {
        public RgceAreaRel(RgceLocRel first, RgceLocRel last)
        {
            this.rowFirst = first.RowIndex;
            this.columnFirst = first.RawColumn;
            this.rowLast = last.RowIndex;
            this.columnLast = last.RawColumn;

        }
#if DEBUG
        public override string ToString()
        {
            return ObjectFormatter.ToString(this, null);
        }
#endif

        public int FirstRowIndex
        {
            get
            {
                if (IsFirstRowRelative)
                    return (short) rowFirst;
                return rowFirst;
            }
        }

        public int FirstColumnIndex
        {
            get { return columnFirst.col;}
        }

        public bool IsFirstRowRelative
        {
            get { return columnFirst.rowRelative; }
        }

        public int LastRowIndex
        {
            get
            {
                if (IsLastRowRelative)
                    return (short) rowLast;
                return rowLast;
            }
        }

        public int LastColumnIndex
        {
            get { return this.columnLast.col; }
        }

        public bool IsLastRowRelative
        {
            get { return this.columnLast.rowRelative; }
        }

        public bool IsLastColumnRelative
        {
            get { return columnLast.columnRelative; }
        }

        public bool IsFirstColumnRelative
        {
            get { return this.columnFirst.columnRelative; }
        }


        public void Read(BinaryReader reader)
        {
            this.rowFirst = reader.ReadUInt16();
            this.rowLast = reader.ReadUInt16();
            columnFirst.Read(reader);
            columnLast.Read(reader);
        }

        private ColRelNegU columnFirst;
        private ColRelNegU columnLast;
        private ushort rowFirst;
        private ushort rowLast;

        public void Write(BinaryWriter writer)
        {
            writer.Write(rowFirst);
            writer.Write(rowLast);
            columnFirst.Write(writer);
            columnLast.Write(writer);
        }
    }
}